package vboot.core.module.sys.coop.corp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vboot.core.common.mvc.dao.Sqler;
import vboot.core.common.mvc.pojo.Zinp;
import vboot.core.common.mvc.service.BaseMainService;
import vboot.core.common.utils.lang.XjsonUtil;
import vboot.core.module.sys.coop.cate.SysCoopCate;
import vboot.core.module.sys.org.root.SysOrg;
import vboot.core.module.sys.org.root.SysOrgRepo;

import javax.annotation.PostConstruct;
import java.util.List;

@Service
public class SysCoopCorpService extends BaseMainService<SysCoopCorp> {

    public String insertx(SysCoopCorp corp){
        String id= insert(corp);
        SysOrg sysOrg=new SysOrg(id,corp.getName(),128);
        orgRepo.save(sysOrg);
        return id;
    }

    public String updatex(SysCoopCorp corp){
        update(corp);
        SysOrg sysOrg=new SysOrg(corp.getId(),corp.getName(),128);
        orgRepo.save(sysOrg);
        return corp.getId();
    }

    public int deletex(String[] ids) {
        for (String id : ids) {
            repo.deleteById(id);
            orgRepo.deleteById(id);
        }
        return ids.length;
    }

    public List<Zinp> findTreeList(String name) {
        Sqler cateSqler = new Sqler("sys_coop_cate");
        cateSqler.addSelect("pid");
        cateSqler.addOrder("t.ornum");
        List<Zinp> list = jdbcDao.findInpList(cateSqler);

        Sqler mainSqler = new Sqler("sys_coop_corp");
        mainSqler.addSelect("t.catid as pid");
        mainSqler.addLike("t.name", name);
        mainSqler.addOrder("t.ornum");
        List<Zinp> list2 = jdbcDao.findInpList(mainSqler);
        list.addAll(list2);

        return XjsonUtil.buildInpByRecursive(list);
    }

    //----------bean注入------------
    @Autowired
    private SysOrgRepo orgRepo;

    @Autowired
    private SysCoopCorpRepo repo;

    @PostConstruct
    public void initDao() {
        super.setRepo(repo);
    }

}
